//
//  MainTabBar.swift
//  SinaWeibo
//
//  Created by Yuan on 16/4/7.
//  Copyright © 2016年 Yuan. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    
    //默认的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
         setupUI()
    }
    
    required init?(coder aDecode: NSCoder){
        //只有默认报错的语句
        //如果有调试这通过，
       // fatalError("init(code:)has ")
        super.init(coder: aDecode)
       
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = self.bounds.width/5
        let h = self.bounds.height
        let rect = CGRect(x: 0,y: -5,width: w,height: h)
        
        var index: CGFloat = 0
        for subview in subviews{
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
                subview.frame = CGRectOffset(rect, index * w, 0)
                if index == 1{
                    index++
                }
                index++
            }
        }
        
        //设置写按钮的位置
        composeBtn.frame = CGRectOffset(rect, w * 2, -0)
        bringSubviewToFront(composeBtn)
    }
    
    private func setupUI()
    {
        addSubview(composeBtn)
    }
    lazy var composeBtn:UIButton = {
        let btn = UIButton()
        
        //设置按钮
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
         btn.setBackgroundImage(UIImage(named: "tabbar_compose_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState:UIControlState.Highlighted)
        
        btn.sizeToFit()
        
        return btn
    }()

}
